//
//  Google_Quote.m
//  MacStockApp
//
//  Created by 英傑 翁 on 12/3/6.
//  Copyright (c) 2012年 翁英傑. All rights reserved.
//

#import "Google_Quote.h"
#import "CommonFunction.h"
#import "GBTableColumnHeader.h"
#import "GBTableColumnHeader_basicSqlite.h"
#import "GBTableColumnHeader_quote.h"
#import "GBTableColumnHeader_2Row3Row.h"
#import "GBTableColumnHeader_node.h" 
#import "GBTableColumnHeader_position.h"  
#import "GBTableColumnHeader_position_bottom_sumaverage.h"  
#import "GBTableColumnHeader_Indicator.h"  
#import "GBTableColumnHeader_signal.h"  
#import "GBTableColumnHeader_ObjectOtherKey.h"  


@implementation Google_Quote

@synthesize delgeate;
- (id)init_WithURL:(NSURL*)url; {
	
    if(self=[super init])
	{
        
        targetURL= url  ;
        [targetURL retain];
    }
	
	return self;
}

//標記：建立執行緒 3.執行緒內的啟動- 在 addOperation 後面執行

- (void)main 
{ 
    NSLog(@"main_Start");
    
    
    //3.Header Field 欺騙bloomberg在User-Agent
    
    
    NSURL* url = targetURL;
    NSString *userAgent = @"Bloomberg-iPad/1.0.9 CFNetwork/548.0.4 Darwin/11.0.0";
    NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url]
                                    autorelease];
    [request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
    //以下可以欺騙yahoo
    //[request setValue:@"vendor=\"fff\";" forHTTPHeaderField:@"X-Client-Info"];    
    
    NSURLResponse* response = nil;
    NSError* error = nil;
    NSData* data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];    
    
    NSString* aStr;
	aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    
    
    //去除無用的字串 '//'
    NSString *string_for_jsonvalue= [[[[[[aStr stringByReplacingOccurrencesOfString: @"//" withString:@"" ] stringByReplacingOccurrencesOfString:@"\\x27" withString:@"\'"]  stringByReplacingOccurrencesOfString:@"\\x26F" withString:@""] stringByReplacingOccurrencesOfString:@"\\x26" withString:@""] stringByReplacingOccurrencesOfString:@"\\x2" withString:@""] stringByReplacingOccurrencesOfString:@"\\x3B" withString:@""]   ;  

    [aStr release];
    
    //將字串的每個elements 分解出來      
    NSArray *jsonvalue_elements = [string_for_jsonvalue JSONValue];
     
    NSMutableArray *outarray=[[NSMutableArray alloc] init];    
    
    //1.建構報價資料陣列    
    for (NSDictionary *dds in jsonvalue_elements ) {
        //NSLog(@"dds=%@",dds);
        
        NSMutableDictionary *tem_dic=[[NSMutableDictionary alloc] init];         
        
        //1.下面資料是yahoo,bloomberg,google 共同有的報價資料
       
        NSString *code_s=[NSString stringWithFormat:@"%@:%@",   [dds objectForKey:@"e"] ,   [dds objectForKey:@"t"] ]; 
         NSNumber *close_num=[NSNumber numberWithDouble: [[[dds objectForKey:@"l"]stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue] ];             
        
        NSNumber *open_num;
       
        if ([[[dds objectForKey:@"op"] stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue]==0){
            open_num=close_num;
         
        }
        else {
            open_num  =[NSNumber numberWithDouble: [[[dds objectForKey:@"op"] stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue] ];
        
        }
      
        
        
        
        NSNumber *high_num;//=[NSNumber numberWithDouble: [[[dds objectForKey:@"hi"] stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue] ];        
        
        if ([[[dds objectForKey:@"hi"] stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue]==0) {
             high_num=close_num;
        }
        else {
            high_num=[NSNumber numberWithDouble: [[[dds objectForKey:@"hi"]stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue] ];     
        }
        
        
        
        NSNumber *low_num;
        if ( [[[dds objectForKey:@"lo"]stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue]  ==0) {
             low_num=close_num;
        }
        else {
             low_num=[NSNumber numberWithDouble: [[[dds objectForKey:@"lo"]stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue] ];
        }
       
        
        
          
        NSNumber *updown_num=[NSNumber numberWithDouble: [[[dds objectForKey:@"c"]stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue] ];
        NSNumber *updown_p_num=[NSNumber numberWithDouble: [[[dds objectForKey:@"cp"]stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue] ];
        
        NSNumber *PE_num=[NSNumber numberWithDouble: [[dds objectForKey:@"pe"] doubleValue] ];
        NSNumber *MarketCap_num;       
      
        //NSLog(@"CLOSE=%@",[dds objectForKey:@"l"]);
        
        if ([[dds objectForKey:@"mc"] hasSuffix:@"M"]) {
            NSString *val=[[dds objectForKey:@"mc"] stringByReplacingOccurrencesOfString:@"M" withString:@""] ;    
            float volf=[val doubleValue]*1000000;
            
            MarketCap_num=[NSNumber numberWithDouble: volf];             
        } 
        else  if ([[dds objectForKey:@"mc"] hasSuffix:@"B"]) {
            NSString *val=[[dds objectForKey:@"mc"] stringByReplacingOccurrencesOfString:@"B" withString:@""] ;    
            float volf=[val doubleValue]*1000000000;
            
            MarketCap_num=[NSNumber numberWithDouble: volf];             
        } 
        else  if ([[dds objectForKey:@"mc"] hasSuffix:@"T"]) {
            NSString *val=[[dds objectForKey:@"mc"] stringByReplacingOccurrencesOfString:@"T" withString:@""] ;    
            float volf=[val doubleValue]*1000000000000;
            
            MarketCap_num=[NSNumber numberWithDouble: volf];             
        } 
        else {
            NSString *val= [[[dds objectForKey:@"mc"] stringByReplacingOccurrencesOfString:@"," withString:@""] stringByReplacingOccurrencesOfString:@".00" withString:@""] ;  
            float volf=[val doubleValue];
            MarketCap_num=[NSNumber numberWithDouble: volf]; 
        }           
        
        NSNumber *low_52week_num=[NSNumber numberWithDouble: [[[dds objectForKey:@"lo52"]stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue] ];
        NSNumber *high_52week_num=[NSNumber numberWithDouble: [[[dds objectForKey:@"hi52"]stringByReplacingOccurrencesOfString:@"," withString:@""] doubleValue] ];        
        
        
        NSDate *datetime_dt=[NSDate dateWithTimeIntervalSince1970:9999];          
        NSNumber *Google_timezone=[NSNumber numberWithDouble: 0 ];        
        
        
        //標記：報價-資料是透過網路上取出報價資料來載入_all 4.google 資料輸入        
        [tem_dic setObject:code_s forKey:DB_object_sqlite_basic_code];
        [tem_dic setObject:open_num forKey:DB_object_quote_all_open];
        [tem_dic setObject:close_num forKey:DB_object_quote_all_close];
        [tem_dic setObject:high_num forKey:DB_object_quote_all_high];
        [tem_dic setObject:low_num forKey:DB_object_quote_all_low];
        [tem_dic setObject:updown_num forKey:DB_object_quote_all_updown];
        [tem_dic setObject:updown_p_num forKey:DB_object_quote_all_updown_p];
        [tem_dic setObject:PE_num forKey:DB_object_quote_all_PE];
        [tem_dic setObject:MarketCap_num forKey:DB_object_quote_all_MarketCap]; 
        [tem_dic setObject:high_52week_num forKey:DB_object_quote_all_high_52week];   
        [tem_dic setObject:low_52week_num forKey:DB_object_quote_all_low_52week];        
        [tem_dic setObject:[NSNumber numberWithDouble: 999 ] forKey:DB_object_quote_all_sample1];          
        
               
        //1.1時間轉換
        if ([dds objectForKey:@"lt"]) {
            datetime_dt=[self google_quote_datetime_string:[dds objectForKey:@"lt"]];
            int a;
            a= [self google_quote_timezone_string:[dds objectForKey:@"lt"]] ;
            Google_timezone=[NSNumber numberWithInt:a ]; 
            
                 
        
        }
        
        [tem_dic setObject:datetime_dt forKey:DB_object_quote_all_datetime]; 
        [tem_dic setObject:Google_timezone forKey:DB_object_quote_Google_timezone];      
        
     
        
        //1.2成交量轉換
        
        NSNumber *volume_num ;         
        if ([[dds objectForKey:@"vo"] hasSuffix:@"M"]) {
            NSString *val=[[dds objectForKey:@"vo"] stringByReplacingOccurrencesOfString:@"M" withString:@""] ;    
            float volf=[val doubleValue]*1000000;
            
            volume_num=[NSNumber numberWithDouble: volf];             
        } 
        else  if ([[dds objectForKey:@"vo"] hasSuffix:@"B"]) {
            NSString *val=[[dds objectForKey:@"vo"] stringByReplacingOccurrencesOfString:@"B" withString:@""] ;    
            float volf=[val doubleValue]*1000000000;
            
            volume_num=[NSNumber numberWithDouble: volf];             
        } 
        
        else {
             NSString *val= [[[dds objectForKey:@"vo"] stringByReplacingOccurrencesOfString:@"," withString:@""] stringByReplacingOccurrencesOfString:@".00" withString:@""] ;  
             float volf=[val doubleValue];
              volume_num=[NSNumber numberWithDouble: volf]; 
        }
        [tem_dic setObject:volume_num forKey:DB_object_quote_all_vol];        
        
       
        
        //2.Google特有的資料
        //標記：報價-資料是透過網路上取出報價資料來載入_google 4.google 資料輸入 輸入真的值777
        {
            NSDate *Google_after_mkt_time =[NSDate dateWithTimeIntervalSince1970:9999];       
            NSNumber *Google_after_mkt_price_num=[NSNumber numberWithDouble: 0 ];         
            NSNumber *Google_after_mkt_change_num=[NSNumber numberWithDouble: 0 ]; 
            NSNumber *Google_after_mkt_chg_percent_num=[NSNumber numberWithDouble: 0 ];         
            NSNumber *Google_shares_num ;         
            NSNumber *Google_beta_num=[NSNumber numberWithDouble: 0 ]; 
            NSNumber *Google_institution_own_num=[NSNumber numberWithDouble: 0 ];           
            NSNumber *Google_delay_num=[NSNumber numberWithDouble: 0 ];         
            NSNumber *Google_30average_vol_num ; 
            
            // NSLog(@"ddddddd");
            if ([dds objectForKey:@"elt"]) {
                Google_after_mkt_time=[self google_quote_datetime_string:[dds objectForKey:@"elt"]]; 
                
                
            }
            if ([dds objectForKey:@"el"]) {
                Google_after_mkt_price_num=[NSNumber numberWithDouble: [[dds objectForKey:@"el"] doubleValue] ];
            }        
            if ([dds objectForKey:@"ec"]) {
                Google_after_mkt_change_num=[NSNumber numberWithDouble: [[dds objectForKey:@"ec"] doubleValue] ];
            }          
            if ([dds objectForKey:@"ecp"]) {
                Google_after_mkt_chg_percent_num=[NSNumber numberWithDouble: [[dds objectForKey:@"ecp"] doubleValue] ];
            }  
            
            if ([[dds objectForKey:@"shares"] hasSuffix:@"M"]) {
                NSString *val=[[dds objectForKey:@"shares"] stringByReplacingOccurrencesOfString:@"M" withString:@""] ;    
                float volf=[val doubleValue]*1000000;
                
                Google_shares_num=[NSNumber numberWithDouble: volf];             
            } 
            else  if ([[dds objectForKey:@"shares"] hasSuffix:@"B"]) {
                NSString *val=[[dds objectForKey:@"shares"] stringByReplacingOccurrencesOfString:@"B" withString:@""] ;    
                float volf=[val doubleValue]*1000000000;
                
                Google_shares_num=[NSNumber numberWithDouble: volf];             
            } 
            else  if ([[dds objectForKey:@"shares"] hasSuffix:@"T"]) {
                NSString *val=[[dds objectForKey:@"shares"] stringByReplacingOccurrencesOfString:@"T" withString:@""] ;    
                float volf=[val doubleValue]*1000000000000;
                
                Google_shares_num=[NSNumber numberWithDouble: volf];             
            } 
            else {
                NSString *val= [[[dds objectForKey:@"shares"] stringByReplacingOccurrencesOfString:@"," withString:@""] stringByReplacingOccurrencesOfString:@".00" withString:@""] ;  
                float volf=[val doubleValue];
                Google_shares_num=[NSNumber numberWithDouble: volf]; 
            }        
            if ([dds objectForKey:@"beta"]) {
                Google_beta_num=[NSNumber numberWithDouble: [[dds objectForKey:@"beta"] doubleValue] ];
            }          
            
            
            if ([dds objectForKey:@"inst_own"]) {
                Google_institution_own_num=[NSNumber numberWithDouble: [[[dds objectForKey:@"inst_own"]  stringByReplacingOccurrencesOfString:@"%" withString:@""] doubleValue] ];
            }         
            
            if ([dds objectForKey:@"delay"]) {
                Google_delay_num=[NSNumber numberWithDouble: [[[dds objectForKey:@"delay"]  stringByReplacingOccurrencesOfString:@"%" withString:@""] doubleValue] ];
            }             
            
            if ([[dds objectForKey:@"avvo"] hasSuffix:@"M"]) {
                NSString *val=[[dds objectForKey:@"avvo"] stringByReplacingOccurrencesOfString:@"M" withString:@""] ;    
                float volf=[val doubleValue]*1000000;
                
                Google_30average_vol_num=[NSNumber numberWithDouble: volf];             
            } 
            else  if ([[dds objectForKey:@"avvo"] hasSuffix:@"B"]) {
                NSString *val=[[dds objectForKey:@"avvo"] stringByReplacingOccurrencesOfString:@"B" withString:@""] ;    
                float volf=[val doubleValue]*1000000000;
                
                Google_30average_vol_num=[NSNumber numberWithDouble: volf];             
            } 
            else  if ([[dds objectForKey:@"avvo"] hasSuffix:@"T"]) {
                NSString *val=[[dds objectForKey:@"avvo"] stringByReplacingOccurrencesOfString:@"T" withString:@""] ;    
                float volf=[val doubleValue]*1000000000000;
                
                Google_30average_vol_num=[NSNumber numberWithDouble: volf];             
            }         else {
                NSString *val= [[[dds objectForKey:@"avvo"] stringByReplacingOccurrencesOfString:@"," withString:@""] stringByReplacingOccurrencesOfString:@".00" withString:@""] ;  
                float volf=[val doubleValue];
                Google_30average_vol_num=[NSNumber numberWithDouble: volf]; 
            }      
            
            
            
            
            [tem_dic setObject:Google_after_mkt_time forKey:DB_object_quote_Google_after_mkt_time];         
            [tem_dic setObject:Google_after_mkt_price_num forKey:DB_object_quote_Google_after_mkt_price];  
            [tem_dic setObject:Google_after_mkt_change_num forKey:DB_object_quote_Google_after_mkt_change];         
            [tem_dic setObject:Google_after_mkt_chg_percent_num forKey:DB_object_quote_Google_after_mkt_chg_percent];  
            [tem_dic setObject:Google_shares_num forKey:DB_object_quote_Google_shares]; 
            [tem_dic setObject:Google_beta_num forKey:DB_object_quote_Google_beta];  
            [tem_dic setObject:Google_institution_own_num forKey:DB_object_quote_Google_institution_own];         
            
            [tem_dic setObject:Google_delay_num forKey:DB_object_quote_Google_delay];  
            [tem_dic setObject:Google_30average_vol_num forKey:DB_object_quote_Google_30average_vol];      
            [tem_dic setObject:[NSNumber numberWithInt:777] forKey:DB_object_quote_Google_sample1];      
            
        }
        
              
        //2.下面資料是bloomberg 獨有
        //標記：報價-資料是透過網路上取出報價資料來載入_bloomberg 4.google 資料輸入 輸入假的值  111
        {
            NSString *introduce= @"";        
            NSNumber *return_52week_num=[NSNumber numberWithDouble: 0 ];        
            
            [tem_dic setObject:introduce forKey:DB_object_quote_Bloomberg_introduce];        
            [tem_dic setObject:return_52week_num forKey:DB_object_quote_Bloomberg_return_52week];         
            [tem_dic setObject:[NSNumber numberWithDouble:111 ] forKey:DB_object_quote_Bloomberg_sample1]; 
            
        }
        
     
        //3.下面資料是YAHOO 獨有
        //標記：報價-資料是透過網路上取出報價資料來載入_yahoo 4.google 資料輸入 輸入假的值  111      
        {
            NSString *currency_s=@"";
            NSString *exchange_s=@"";
            
            NSNumber *dividend_rate_num=[NSNumber numberWithDouble: 0 ]; 
            NSNumber *dividend_yield_num=[NSNumber numberWithDouble:0 ];       
            NSNumber *eps_curr_year_num=[NSNumber numberWithDouble: 0];
            NSNumber *realtime_change_num=[NSNumber numberWithDouble: 0 ]; 
            NSNumber *realtime_chg_percent_num=[NSNumber numberWithDouble: 0];       
            NSNumber *realtime_price_num=[NSNumber numberWithDouble: 0];
            NSDate   *realtime_ts=[NSDate dateWithTimeIntervalSince1970:0 ]; 
            NSNumber *after_mkt_change_num=[NSNumber numberWithDouble: 0 ]; 
            NSNumber *after_mkt_chg_percent_num=[NSNumber numberWithDouble:0 ];       
            NSNumber *after_mkt_price_num=[NSNumber numberWithDouble:0 ];
            
            [tem_dic setObject:after_mkt_change_num forKey:DB_object_quote_Yahoo_after_mkt_change];   
            [tem_dic setObject:after_mkt_chg_percent_num forKey:DB_object_quote_Yahoo_after_mkt_chg_percent];
            [tem_dic setObject:after_mkt_price_num forKey:DB_object_quote_Yahoo_after_mkt_price];             
            [tem_dic setObject:currency_s forKey:DB_object_quote_Yahoo_currency];   
            [tem_dic setObject:exchange_s forKey:DB_object_quote_Yahoo_exchange];
            [tem_dic setObject:dividend_rate_num forKey:DB_object_quote_Yahoo_dividend_rate];               
            [tem_dic setObject:dividend_yield_num forKey:DB_object_quote_Yahoo_dividend_yield];         
            [tem_dic setObject:eps_curr_year_num forKey:DB_object_quote_Yahoo_eps_curr_year]; 
            [tem_dic setObject:realtime_change_num forKey:DB_object_quote_Yahoo_realtime_change];   
            [tem_dic setObject:realtime_chg_percent_num forKey:DB_object_quote_Yahoo_realtime_chg_percent];
            [tem_dic setObject:realtime_price_num forKey:DB_object_quote_Yahoo_realtime_price];               
            [tem_dic setObject:realtime_ts forKey:DB_object_quote_Yahoo_realtime_ts];          
            
            [tem_dic setObject:[NSNumber numberWithDouble:111 ] forKey:DB_object_quote_Yahoo_sample1];            
            
        }
         
        
        //最後添加到陣列
        [outarray addObject:tem_dic];   
        [tem_dic release];
            
    }              
   
    //2.將報價資料陣列更新到物件上
    [CommonFunction after_quotefinished_outarray:outarray delgeate:delgeate];      
    
    
    
    //標記：建立執行緒 4.執行緒內資料計算結束後 要通知delgete    
    [(Data_Model *)delgeate performSelectorOnMainThread:@selector(Quote_Finished:) withObject:outarray waitUntilDone:YES];    
    
    
    
    [outarray release];     
    
    
}
//標記:將三個英文字母的月份轉為數字
-(int)MMM_MonthConvertToInterger:(NSString *)mmm{
    NSString *MMMs;
    MMMs=[mmm uppercaseString];
    if ([MMMs isEqualToString:[@"Jan" uppercaseString] ]) {
        return 1;
    }
    else if ([MMMs isEqualToString:[@"Feb" uppercaseString] ]) {
        return 2;
    }  
    else if ([MMMs isEqualToString:[@"Mar" uppercaseString] ]) {
        return 3;
    }  
    else if ([MMMs isEqualToString:[@"Apr" uppercaseString] ]) {
        return 4;
    }  
    else if ([MMMs isEqualToString:[@"May" uppercaseString] ]) {
        return 5;
    }  
    else if ([MMMs isEqualToString:[@"Jun" uppercaseString] ]) {
        return 6;
    }  
    else if ([MMMs isEqualToString:[@"Jul" uppercaseString] ]) {
        return 7;
    } 
    else if ([MMMs isEqualToString:[@"Aug" uppercaseString] ]) {
        return 8;
    }  
    else if ([MMMs isEqualToString:[@"Sep" uppercaseString] ]) {
        return 9;
    }  
    else if ([MMMs isEqualToString:[@"Oct" uppercaseString] ]) {
        return 10;
    }      
    else if ([MMMs isEqualToString:[@"Nov" uppercaseString] ]) {
        return 11;
    }  
    else if ([MMMs isEqualToString:[@"Dec" uppercaseString] ]) {
        return 12;
    }  
    else{
        return 0;
        
    }
    
}

-(NSDate *)google_quote_datetime_string:(NSString *)dss{
    
    
    
        NSString *time_String=dss;
        NSArray * Timestamp_array=[time_String componentsSeparatedByString:@","];             
        int year=1970;
        int month=1;
        int day=1;
        int hour=0;
        int minute=0;
        float gmt=0;
        
        if ([Timestamp_array count]==2) {
            //日期 year month day
            {
                NSString *dates=[Timestamp_array objectAtIndex:0];   
                
                //year 透過系統時間取得                   
                NSDate *date=[NSDate date];
                NSDateFormatter *dateFormatter_yyyy = [[NSDateFormatter alloc]init];                        
                [dateFormatter_yyyy setDateFormat:@"yyyy"];
                NSString *year_google=[dateFormatter_yyyy stringFromDate:date];
                [dateFormatter_yyyy release]; 
                year=[year_google intValue];                
                
                //月 日
                NSArray * date_array=[dates componentsSeparatedByString:@" "]; 
                
                if ([date_array count]==2) {
                    month=[self MMM_MonthConvertToInterger:[date_array objectAtIndex:0]];
                    day=[[date_array objectAtIndex:1] intValue];
                }
                
                
            }
            //時間 hour minute
            {
                NSString *times=[Timestamp_array objectAtIndex:1];   
                NSArray *Times_array=[times componentsSeparatedByString:@" "];      
                if ([Times_array count]==3) {
                    NSLog(@"時間=%@ 時區=%@",[Times_array objectAtIndex:1],[Times_array objectAtIndex:2]);
                    //判斷上下午
                    if ([[Times_array objectAtIndex:1] hasSuffix:@"PM"]) {
                        //NSLog(@"PM"); 
                        NSString *hm=[[Times_array objectAtIndex:1] stringByReplacingOccurrencesOfString:@"PM" withString:@""];
                        NSArray *hma=[hm componentsSeparatedByString:@":"];
                        if ([hma count]==2) {
                          
                            //因為 如果在中午的時間 格式如下：時間=12:37PM 時區=GMT+08:00 所以必須透過下面的調整，避免在中午12點的錯誤
                            
                            if ([[hma objectAtIndex:0] intValue]==12) {
                                hour=[[hma objectAtIndex:0] intValue]; //不增加12
                                minute= [[hma objectAtIndex:1] intValue];
                            }
                            else{
                                hour=12+[[hma objectAtIndex:0] intValue];
                                minute= [[hma objectAtIndex:1] intValue];
                            }
                           
                            
                        }
                        
                        
                    }
                    else if([[Times_array objectAtIndex:1] hasSuffix:@"AM"]){
                        //NSLog(@"AM"); 
                        NSString *hm=[[Times_array objectAtIndex:1] stringByReplacingOccurrencesOfString:@"AM" withString:@""];
                        NSArray *hma=[hm componentsSeparatedByString:@":"];
                        if ([hma count]==2) {
                            hour= [[hma objectAtIndex:0] intValue];
                            minute= [[hma objectAtIndex:1] intValue];                                    
                        }                            
                        
                    }
                    //判斷時區
                    if ([[Times_array objectAtIndex:2] isEqualToString:@"GMT"]) {
                        gmt=0;
                    }
                    else if ([[Times_array objectAtIndex:2] hasPrefix:@"GMT+"]){
                        
                        NSArray *gmt_array=[[[Times_array objectAtIndex:2] stringByReplacingOccurrencesOfString:@"GMT+" withString:@""] componentsSeparatedByString:@":"];
                        if ([gmt_array count]==2) {
                            gmt=[[gmt_array objectAtIndex:0] doubleValue]+[[gmt_array objectAtIndex:1] doubleValue]/60;
                        }
                        
                    }
                    else if ([[Times_array objectAtIndex:2] hasPrefix:@"GMT-"]){
                        NSArray *gmt_array=[[[Times_array objectAtIndex:2] stringByReplacingOccurrencesOfString:@"GMT-" withString:@""] componentsSeparatedByString:@":"];
                        if ([gmt_array count]==2) {
                            gmt=-1*([[gmt_array objectAtIndex:0] doubleValue]+[[gmt_array objectAtIndex:1] doubleValue]/60);
                        }
                    } 
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"CET"] ||  [[Times_array objectAtIndex:2] isEqualToString:@"FWT"] || [[Times_array objectAtIndex:2] isEqualToString:@"MET"] || [[Times_array objectAtIndex:2] isEqualToString:@"MEWT"] || [[Times_array objectAtIndex:2] isEqualToString:@"SWT"] || [[Times_array objectAtIndex:2] isEqualToString:@"BST"] || [[Times_array objectAtIndex:2] isEqualToString:@"IST"] || [[Times_array objectAtIndex:2] isEqualToString:@"WEDT"] || [[Times_array objectAtIndex:2] isEqualToString:@"WEST"]){
                        gmt=1;
                    }                        
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"EET"] ||  [[Times_array objectAtIndex:2] isEqualToString:@"CEDT"] || [[Times_array objectAtIndex:2] isEqualToString:@"CEST"] || [[Times_array objectAtIndex:2] isEqualToString:@"MEST"] || [[Times_array objectAtIndex:2] isEqualToString:@"MESZ"] || [[Times_array objectAtIndex:2] isEqualToString:@"SST"] || [[Times_array objectAtIndex:2] isEqualToString:@"FST"]  ){
                        gmt=2;
                    }                       
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"BT"]|| [[Times_array objectAtIndex:2] isEqualToString:@"EEDT"] || [[Times_array objectAtIndex:2] isEqualToString:@"EEST"] ){
                        gmt=3;
                    } 
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"ZP4"] ){
                        gmt=4;
                    }                       
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"ZP5"] ){
                        gmt=5;
                    }       
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"ZP6"] ){
                        gmt=6;
                    }                      
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"CXT"] ){
                        gmt=7;
                    }                      
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"CCT"] ||  [[Times_array objectAtIndex:2] isEqualToString:@"AWST"] || [[Times_array objectAtIndex:2] isEqualToString:@"WST"] || [[Times_array objectAtIndex:2] isEqualToString:@"WADT"]  ){
                        gmt=8;
                    }                       
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"JST"] ){
                        gmt=9;
                    }                      
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"EAST"] ||    [[Times_array objectAtIndex:2] isEqualToString:@"GST"]  ){
                        gmt=10;
                    }                     
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"AEDT"]  ||  [[Times_array objectAtIndex:2] isEqualToString:@"EADT"]    ){
                        gmt=11;
                    }                      
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"IDLE"] ||  [[Times_array objectAtIndex:2] isEqualToString:@"NZST"] || [[Times_array objectAtIndex:2] isEqualToString:@"NZT"] ){
                        gmt=12;
                    }                       
                    
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"NZDT"]){
                        gmt=13;
                    }                        
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"EDT"]){
                        gmt=-4;
                    }                    
                    
                    else if ([[Times_array objectAtIndex:2] isEqualToString:@"EST"]){
                        gmt=-5;
                    }
                     
                    else {
                        NSLog(@"****沒有照到對應的時間格式*******************=%@",[Times_array objectAtIndex:2] );
                    }
                    
                }
                else {
                   // NSLog(@"時區時間格式錯誤=%@",times);
                }
                
                
            }
        }
        else {
           // NSLog(@"日期時間格式錯誤=%@",time_String);                
        }
        
        //NSLog(@"%i/%i/%i %i:%i GMT=%f",year,month,day,hour,minute,gmt);
        
        //轉成 NSDate
        {
            NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
            //假設yahoo 的資料員都是設定從紐約來的
            [gregorian setTimeZone: [NSTimeZone timeZoneForSecondsFromGMT:3600*gmt]];
            
            NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];
            [timeZoneComps setYear:year];
            [timeZoneComps setMonth:month];
            [timeZoneComps setDay:day];
            [timeZoneComps setHour:hour];
            [timeZoneComps setMinute:minute];
            NSDate *date_out=[gregorian dateFromComponents:timeZoneComps];                          
            [gregorian release];
            [timeZoneComps release];            
            
            return date_out;
        }
        
        
    

}
//取出時區
-(int)google_quote_timezone_string:(NSString *)dss{
    
    NSString *time_String=dss;
    NSArray * Timestamp_array=[time_String componentsSeparatedByString:@","];             
    int year=1970;
    int month=1;
    int day=1;
    int hour=0;
    int minute=0;
    float gmt=0;
    
    if ([Timestamp_array count]==2) {
        //日期 year month day
        {
            NSString *dates=[Timestamp_array objectAtIndex:0];   
            
            //year 透過系統時間取得                   
            NSDate *date=[NSDate date];
            NSDateFormatter *dateFormatter_yyyy = [[NSDateFormatter alloc]init];                        
            [dateFormatter_yyyy setDateFormat:@"yyyy"];
            NSString *year_google=[dateFormatter_yyyy stringFromDate:date];
            [dateFormatter_yyyy release]; 
            year=[year_google intValue];                
            
            //月 日
            NSArray * date_array=[dates componentsSeparatedByString:@" "]; 
            
            if ([date_array count]==2) {
                month=[self MMM_MonthConvertToInterger:[date_array objectAtIndex:0]];
                day=[[date_array objectAtIndex:1] intValue];
            }
            
            
        }
        //時間 hour minute
        {
            NSString *times=[Timestamp_array objectAtIndex:1];   
            NSArray *Times_array=[times componentsSeparatedByString:@" "];      
            if ([Times_array count]==3) {
                //NSLog(@"時間=%@ 時區=%@",[Times_array objectAtIndex:1],[Times_array objectAtIndex:2]);  
                //判斷上下午
                if ([[Times_array objectAtIndex:1] hasSuffix:@"PM"]) {
                    //NSLog(@"PM"); 
                    NSString *hm=[[Times_array objectAtIndex:1] stringByReplacingOccurrencesOfString:@"PM" withString:@""];
                    NSArray *hma=[hm componentsSeparatedByString:@":"];
                    if ([hma count]==2) {
                        hour=12+[[hma objectAtIndex:0] intValue];
                        minute= [[hma objectAtIndex:1] intValue];                             
                    }
                    
                    
                }
                else if([[Times_array objectAtIndex:1] hasSuffix:@"AM"]){
                    //NSLog(@"AM"); 
                    NSString *hm=[[Times_array objectAtIndex:1] stringByReplacingOccurrencesOfString:@"AM" withString:@""];
                    NSArray *hma=[hm componentsSeparatedByString:@":"];
                    if ([hma count]==2) {
                        hour= [[hma objectAtIndex:0] intValue];
                        minute= [[hma objectAtIndex:1] intValue];                                    
                    }                            
                    
                }
                //判斷時區
                if ([[Times_array objectAtIndex:2] isEqualToString:@"GMT"]) {
                    gmt=0;
                }
                else if ([[Times_array objectAtIndex:2] hasPrefix:@"GMT+"]){
                    
                    NSArray *gmt_array=[[[Times_array objectAtIndex:2] stringByReplacingOccurrencesOfString:@"GMT+" withString:@""] componentsSeparatedByString:@":"];
                    if ([gmt_array count]==2) {
                        gmt=[[gmt_array objectAtIndex:0] doubleValue]+[[gmt_array objectAtIndex:1] doubleValue]/60;
                    }
                    
                }
                else if ([[Times_array objectAtIndex:2] hasPrefix:@"GMT-"]){
                    NSArray *gmt_array=[[[Times_array objectAtIndex:2] stringByReplacingOccurrencesOfString:@"GMT-" withString:@""] componentsSeparatedByString:@":"];
                    if ([gmt_array count]==2) {
                        gmt=-1*([[gmt_array objectAtIndex:0] doubleValue]+[[gmt_array objectAtIndex:1] doubleValue]/60);
                    }
                } 
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"CET"] ||  [[Times_array objectAtIndex:2] isEqualToString:@"FWT"] || [[Times_array objectAtIndex:2] isEqualToString:@"MET"] || [[Times_array objectAtIndex:2] isEqualToString:@"MEWT"] || [[Times_array objectAtIndex:2] isEqualToString:@"SWT"] || [[Times_array objectAtIndex:2] isEqualToString:@"BST"] || [[Times_array objectAtIndex:2] isEqualToString:@"IST"] || [[Times_array objectAtIndex:2] isEqualToString:@"WEDT"] || [[Times_array objectAtIndex:2] isEqualToString:@"WEST"]){
                    gmt=1;
                }                        
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"EET"] ||  [[Times_array objectAtIndex:2] isEqualToString:@"CEDT"] || [[Times_array objectAtIndex:2] isEqualToString:@"CEST"] || [[Times_array objectAtIndex:2] isEqualToString:@"MEST"] || [[Times_array objectAtIndex:2] isEqualToString:@"MESZ"] || [[Times_array objectAtIndex:2] isEqualToString:@"SST"] || [[Times_array objectAtIndex:2] isEqualToString:@"FST"]  ){
                    gmt=2;
                }                       
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"BT"]|| [[Times_array objectAtIndex:2] isEqualToString:@"EEDT"] || [[Times_array objectAtIndex:2] isEqualToString:@"EEST"] ){
                    gmt=3;
                } 
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"ZP4"] ){
                    gmt=4;
                }                       
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"ZP5"] ){
                    gmt=5;
                }       
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"ZP6"] ){
                    gmt=6;
                }                      
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"CXT"] ){
                    gmt=7;
                }                      
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"CCT"] ||  [[Times_array objectAtIndex:2] isEqualToString:@"AWST"] || [[Times_array objectAtIndex:2] isEqualToString:@"WST"] || [[Times_array objectAtIndex:2] isEqualToString:@"WADT"]  ){
                    gmt=8;
                }                       
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"JST"] ){
                    gmt=9;
                }                      
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"EAST"] ||    [[Times_array objectAtIndex:2] isEqualToString:@"GST"]  ){
                    gmt=10;
                }                     
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"AEDT"]  ||  [[Times_array objectAtIndex:2] isEqualToString:@"EADT"]    ){
                    gmt=11;
                }                      
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"IDLE"] ||  [[Times_array objectAtIndex:2] isEqualToString:@"NZST"] || [[Times_array objectAtIndex:2] isEqualToString:@"NZT"] ){
                    gmt=12;
                }                       
                
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"NZDT"]){
                    gmt=13;
                }                        
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"EDT"]){
                    gmt=-4;
                }                    
                
                else if ([[Times_array objectAtIndex:2] isEqualToString:@"EST"]){
                    gmt=-5;
                }
                
                else {
                    NSLog(@"****沒有照到對應的時間格式*******************=%@",[Times_array objectAtIndex:2] );
                }
                
            }
            else {
                // NSLog(@"時區時間格式錯誤=%@",times);
            }
            
            
        }
    }
    else {
        // NSLog(@"日期時間格式錯誤=%@",time_String);                
    }
    
    NSLog(@"%i/%i/%i %i:%i GMT=%f",year,month,day,hour,minute,gmt);
    return gmt;
    
}
 



@end
